﻿using System;
using UnityEngine;

namespace BF
{
    public abstract class ObjectRecycleItem : IRecycleable, IDisposable
    {
        /// <summary>
        /// 是否已经被回收
        /// </summary>
        public bool isRecycled { get; set; }
        /// <summary>
        /// 是否是第一次从对象池中取出
        /// </summary>
        bool IRecycleable.isFirstShow { get; set; }
        /// <summary>
        /// 对象回收到池中的时间
        /// </summary>
        float IRecycleable.recycleTime { get; set; }
        /// <summary>
        /// 回收时的回调
        /// </summary>
        private event Action _onRecycle;

        private IPool _pool;

        IPool IRecycleable.pool { get => _pool; set => _pool = value; }

        public virtual void OnFirstShow()
        {

        }

        public virtual void OnShow()
        {
        }

        /// <summary>
        /// 添加对象回收时的回调，调用完后会置空。
        /// </summary>
        /// <param name="callback">回调</param>
        public void OnRecycle(Action callback)
        {
            this._onRecycle += callback;
        }

        public virtual void OnRecycle()
        {
            _onRecycle?.Invoke();
            _onRecycle = null;
        }

        void IRecycleable.Destroy()
        {
            Dispose();
        }

        public virtual void Dispose()
        {

        }

        public void ReturnToPool()
        {
            _pool.Push(this);
        }
    }

    [System.Obsolete("Use ObjectRecycleItem instead")]
    public abstract class ObjectRecycleItem<T> : ObjectRecycleItem where T : class, IRecycleable
    {

    }
}
